package cn.stalk.compare.ibatis;

import cn.stalk.compare.DataComparator;
import cn.stalk.compare.StreamComparator;
import cn.stalk.compare.result.ComparisonResult;
import org.apache.ibatis.session.ResultHandler;

import java.util.function.Consumer;

/**
 * 适用于ibatis流式查询的数据比较器
 */
public class ResultStreamComparator<T> extends StreamComparator<T> {

    public ResultStreamComparator(DataComparator<T> dataComparator, Consumer<ComparisonResult<T>> handler) {
        super(dataComparator, handler);
    }

    public ResultStreamComparator(DataComparator<T> dataComparator, Consumer<ComparisonResult<T>> handler, int capacity) {
        super(dataComparator, handler, capacity);

    }

    /**
     * 构建源数据的处理器
     */
    public ResultHandler<T> buildSourceHandler() {
        return resultContext -> {
            pushSource(resultContext.getResultObject());
            if (resultContext.isStopped()) {
                stopSource();
            }
        };
    }

    /**
     * 构建目标数据的处理器
     */
    public ResultHandler<T> buildTargetHandler() {
        return resultContext -> {
            pushTarget(resultContext.getResultObject());
            if (resultContext.isStopped()) {
                stopTarget();
            }
        };
    }
}
